﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using Microsoft.SharePoint.Administration;

namespace MicroLink.StsDev2008AddIn.BuildTasks
{
    public class AddSolution : Task
    {
        #region FileName
        [Required()]
        public string FileName
        {
            get;
            set;
        }
        #endregion

        #region Location
        [Required()]
        public string Location
        {
            get;
            set;
        }
        #endregion

        public override bool Execute()
        {
            SPSolution solution = SPFarm.Local.Solutions[FileName];
            if (solution == null)
            {
                try
                {
                    solution = SPFarm.Local.Solutions.Add(Location + FileName);
                    if (solution != null)
                        return true;
                    else
                    {
                        Log.LogError("{0} did not register as a solution", FileName);
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    Log.LogError("ERROR: {0}", ex.Message);
                    return false;
                }
            }
            else
            {
                this.Log.LogError("{0} already exists in the solution store.", FileName);
                return false;
            }
        }
    }
}
